package cn.com.kh;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;

public class FileCharacterReplacement {
    private static final Log log = LogFactory.getLog(FileCharacterReplacement.class);
    public static void main(String[] args) throws IOException {
        String filePath = "C:\\Users\\LiJT\\Desktop\\民生加银基金对账单.html"; // 指定文件路径

        try (BufferedReader reader = new BufferedReader(new FileReader(filePath)); PrintWriter writer = new PrintWriter(new FileWriter("tempFile"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                // 将需要替换的字符进行替换操作
                String replacedLine = line.replace("http://www.msjyfund.com.cn/data/kefu/seal_small.png", "http://www.msjyfund.com.cn/data/kefu/seal.png");
                // 将邮件正文内容中的外网URL换成本地(服务器无法访问外网)
                replacedLine = replacedLine.replace("http://www.msjyfund.com.cn/data/kefu/", "http://127.0.0.1:8010/ngs/fs/get?file=sms/images/");

                // 输出到临时文件
                writer.println(replacedLine);
            }
        } catch (IOException e) {
            log.error("发生错误：" + e.getMessage());
        } finally {
            // 删除原始文件，重命名临时文件为原始文件名
            File originalFile = new File(filePath);
            File tempFile = new File("tempFile");
            if (!originalFile.delete() || !tempFile.renameTo(originalFile)) {
                log.warn("无法完成文件替换！");
            } else {

                // 获取文件的绝对路径
                filePath = tempFile.getAbsolutePath();

                // 输出文件路径
                log.debug("文件替换成功！创建的文件路径是：" + filePath);
            }

        }
    }
}